/* add user code begin Header */
/**
  **************************************************************************
  * @file     main.c
  * @brief    main program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */
/* add user code end Header */

/* Includes ------------------------------------------------------------------*/
#include "at32f415_wk_config.h"

/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#include "cx_timer.h"
#include "cx_task.h"
/* add user code end private includes */

/* private typedef -----------------------------------------------------------*/
/* add user code begin private typedef */

/* add user code end private typedef */

/* private define ------------------------------------------------------------*/
/* add user code begin private define */

/* add user code end private define */

/* private macro -------------------------------------------------------------*/
/* add user code begin private macro */

/* add user code end private macro */

/* private variables ---------------------------------------------------------*/
/* add user code begin private variables */

/* add user code end private variables */

/* private function prototypes --------------------------------------------*/
/* add user code begin function prototypes */

/* add user code end function prototypes */

/* private user code ---------------------------------------------------------*/
/* add user code begin 0 */

#define TASK1_STACK_SIZE                    128
StackType_t Task1Stack[TASK1_STACK_SIZE];
#define TASK2_STACK_SIZE                    128
StackType_t Task2Stack[TASK2_STACK_SIZE];
void LED0_toggle(void *args)
{
  if(gpio_output_data_bit_read(GPIOA, GPIO_PINS_1))
  {
    gpio_bits_reset(GPIOA,GPIO_PINS_1);
  }
  else
  {
    gpio_bits_set(GPIOA, GPIO_PINS_1);
  }
}

void LED1_toggle(void *args)
{
  if(gpio_output_data_bit_read(GPIOA, GPIO_PINS_2))
  {
    gpio_bits_reset(GPIOA,GPIO_PINS_2);
  }
  else
  {
    gpio_bits_set(GPIOA, GPIO_PINS_2);
  }
}

extern cx_timer_interface_t cx_timer0;

cx_software_timer_t LED_toggle_sw = 
{
  .cb_handler = LED0_toggle,
  .args = CX_NULL,
  .gap = 99,
};
long long next=250;

// void SysTick_Delay_Ms( __IO uint32_t ms) 
// {
// 	 uint32_t i;
// 	 SysTick_Config(SystemCoreClock/1000); 
// 	 for (i=0; i<ms; i++)
// 	 {	
// 	 	// 当计数器的值减小到 0 的时候，CRTL 寄存器的位 16 会置 1 
// 	 	// 当置 1 时，读取该位会清 0 
// 	 	while ( !((SysTick->CTRL)&(1<<16)) ); 
// 	 }
// 	 SysTick->CTRL &=~SysTick_CTRL_ENABLE_Msk;
// }

void Task1_Entry( void *p_arg )
{
for ( ;; )
    {
        LED0_toggle(CX_NULL);
        TaskDelay(SYS_MS_TO_TICKS(500));
        LED0_toggle(CX_NULL);
        TaskDelay(SYS_MS_TO_TICKS(500));
    }
}

void Task2_Entry( void *p_arg )
{
for ( ;; )
    {
        LED1_toggle(CX_NULL);
        TaskDelay(SYS_MS_TO_TICKS(500));
        LED1_toggle(CX_NULL);
        TaskDelay(SYS_MS_TO_TICKS(500));
    }
}

extern TCB_t *CurrentTask;
TCB_t Task1,Task2;
void Task1_Entry( void *p_arg );
void Task2_Entry( void *p_arg );

/* add user code end 0 */

/**
  * @brief main function.
  * @param  none
  * @retval none
  */
int main(void)
{
  /* add user code begin 1 */

  /* add user code end 1 */

  /* system clock config. */
  wk_system_clock_config();

  /* config periph clock. */
  wk_periph_clock_config();

  /* init debug function. */
  wk_debug_config();

  /* nvic config. */
  wk_nvic_config();

  /* init usart1 function. */
  wk_usart1_init();

  /* init tmr9 function. */
  wk_tmr9_init();

  /* init tmr10 function. */
  wk_tmr10_init();

  /* init gpio function. */
  wk_gpio_config();

  /* add user code begin 2 */
  #define USE_SELF_RTOS
#ifdef USE_SELF_RTOS
  TaskCreat(&Task1, Task1_Entry, Task1Stack, TASK1_STACK_SIZE,0);
  StartSchedual();
#else
#endif
  /* add user code end 2 */
#include "sys.h"
  while(1)
  {
    /* add user code begin 3 */
    cx_sys_startup1();
    /* add user code end 3 */
  }
}
